<!-- /*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */
  -->

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*, loginbean.*" %>
<%
	ManageObjects manager = new ManageObjects();
	if(request.getParameter("Submit").trim().equals("Change Object"))
	{
		if(request.getParameter("newObjectName").length() != 0)
			manager.editObject(new Integer(request.getParameter("selectedObject")).intValue(), request.getParameter("newObjectName"));
	}
	else if(request.getParameter("Submit").trim().equals("Delete Field"))
	{
		System.out.println(request.getParameter("selectedField"));
		manager.deleteField(new Integer(request.getParameter("selectedField")).intValue());
	}
	else if(request.getParameter("Submit").trim().equals("Change Fields"))
	{
		for(Enumeration e = request.getParameterNames(); e.hasMoreElements();)
		{
			String param = (String)e.nextElement();
			if(request.getParameter(param).length() > 0)
			{
				int fieldId;
				if(param.startsWith("newTypeName"))
				{
					fieldId = new Integer(param.substring(11)).intValue();
					manager.editField(fieldId, "", request.getParameter(param));
				}
				else if(param.startsWith("newFieldName"))
				{
					fieldId = new Integer(param.substring(12)).intValue();
					manager.editField(fieldId, request.getParameter(param), "");
				}
			}
		}
		manager.closeDb();
	}
%>
<%ObjectMap objectList = new ObjectMap(new Integer(session.getAttribute("tenantid").toString()).intValue()); %>
<html>
<head>
<%
	if(session.getAttribute("username")==null || session.getAttribute("username")=="")
	{
			response.sendRedirect("index.jsp");
	}
%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Edit Object Page</title>
<SCRIPT language="JavaScript">

function deleteField( fldId )
{
	document.editFieldForm.selectedField.value=fldId;
}

</SCRIPT>
<link href="twoColElsLtHdr.css" rel="stylesheet" type="text/css" /><!--[if IE]>
<style type="text/css"> 
/* place css fixes for all versions of IE in this conditional comment */
.twoColElsLtHdr #sidebar1 { padding-top: 30px; }
.twoColElsLtHdr #mainContent { zoom: 1; padding-top: 15px; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */


</style>
<![endif]-->
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
</head>

<body class="twoColElsLtHdr">

<div id="container">
  <div id="header">
    <h1><a href="welcome.jsp"><img src="saas-model.jpg" width="206" height="138" hspace="5" align="left" /></a>Anand, Bryce, Chao's Mini-SaaS</h1>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  <!-- end #header --></div>
  <div id="sidebar1">
    <ul id="MenuBar1" class="MenuBarVertical">
	  <li><a class="MenuBarItemSubmenu" href="welcome.jsp">Home</a>
      <li><a class="MenuBarItemSubmenu" href="#">Object Menu</a>
        <ul>
          <li><a href="createObject.jsp">Create Custom</a></li>
          <li><a href="display.jsp">Display</a></li>
</ul>
      </li>
      <li><a href="#" class="MenuBarItemSubmenu">WorkFlow Menu</a>
        <ul>
          <li><a href="CreateWorkFlow.jsp">Create New</a></li>
          <li><a href="CustomizeWorkFlow.jsp">Customize</a></li>
        </ul>
      </li>
      <li><a class="MenuBarItemSubmenu" href="#">WebService Menu</a>
        <ul>
          <li><a href="CreateWebService.jsp">Create New</a>          </li>
          <li><a href="displayAvailableWebServices.jsp">Display</a></li>
          <li><a href="searchWebServices.jsp">Search</a></li>
        </ul>
      </li>
      <li><a href="#" class="MenuBarItemSubmenu">GUI</a>
        <ul>
          <li><a href="CustomizeGUI.jsp">Customize</a></li>
        </ul>
      </li>
      <li><a class="MenuBarItemSubmenu" href="Logout.jsp">Log Out</a>
    </ul>
  </div>
  <div id="mainContent">
<p>
On this page you can edit your custom object.  You Can change the name in the top section by typing a new name in the field and clicking the change object button.
</p>
<p>
In the second section you can change the name of the data members and the data types of each member.  Please do not use this if you already have data in your object as it will create and error in your object.  If you have already entered data, you should delete the object and start over.  Once you have entered new information press the change fields button.  If you would like to delete a field press the delete fields button next to the field.
</p>
<%
	Iterator objectNames = objectList.keySet().iterator();
	while( objectNames.hasNext() )
	{
		String objectId = (String)objectNames.next();
		if(objectId.equals(request.getParameter("selectedObject")))
		{%>
			<form method="POST" name="nameChangeForm" action="">
			<table border="1">
				<tr>
				<th>Object Name</th><th>New Object Name</th></tr>
				<%		
				ObjectBean obj = (ObjectBean)objectList.get(objectId);
				String objectName = obj.getName();
				out.println("<tr><td>");
				out.println(objectName + "</td><td>");
				%>
				<input type="text" name="newObjectName" value="" /></td>
				<%
				out.print( "<td><input type=\"submit\" name=\"Submit\" value=\"Change Object\" >");
				out.println("</td></tr>");
				%>
				</tr>
			</table>
			<input type="hidden" name="selectedObject" value="<% out.print(objectId); %>">
			</form>
			
			<form method="POST" name="editFieldForm" action="">
			<input type="hidden" name="selectedField" value="">
			<table border="1"> 
			<tr>
			<th>Field Name</th><th>New Field Name</th><th>Type Name</th><th>New Type Name</th><th>Delete Field</th></tr>
			<%
				FieldBean[] fields = obj.getFields();
				for(int i=0; i<fields.length; i++)
				{
					out.println("<tr><td>");
					out.println(fields[i].getName() + "</td><td>");
					%><input type="text" name="newFieldName<%out.print(fields[i].getFieldId()); %>" value="" /></td><%
					
					out.println("<td>");
					out.println(fields[i].getDataType() + "</td><td>");
					%><input type="text" name="newTypeName<%out.print(fields[i].getFieldId()); %>" value="" /></td><%
					
					if(fields.length > 1)
					{						
						out.print( "<td><input type=\"submit\" name=\"Submit\" value=\"Delete Field\" onClick=\"deleteField('"+ fields[i].getFieldId() +"')\"></td>");
					}
					out.println("</tr>");
				}%>
				
				
			</table>
			<input type="submit" name="Submit" value="Change Fields" />
			</form> 
		<%}
	}
%>
  <!-- end #mainContent --></div>
	<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />
   <div id="footer">
    <p>&nbsp;</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>